<html>
<head><meta charset="utf-8"><title>Crate for &quot;divide, rounding up&quot;? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html">Crate for &quot;divide, rounding up&quot;?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211431567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211431567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211431567">(Sep 27 2020 at 19:50)</a>:</h4>
<p>Is there a well-established crate providing the "divide, rounding up" operation?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">0</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">1</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">4095</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">4096</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">4097</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">8191</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="mi">8192</span><span class="k">u64</span><span class="p">.</span><span class="n">div_round_up</span><span class="p">(</span><span class="mi">4096</span><span class="p">),</span><span class="w"> </span><span class="mi">2</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="211431579"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211431579" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211431579">(Sep 27 2020 at 19:51)</a>:</h4>
<p>I have such a crate locally, and I'm wondering if I should upload it.</p>



<a name="211432928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432928">(Sep 27 2020 at 20:23)</a>:</h4>
<p>would rounding up overflow?</p>



<a name="211432970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432970">(Sep 27 2020 at 20:24)</a>:</h4>
<p>In theory, it shouldn't ever be able to overflow, no, since the division should always make it smaller.</p>



<a name="211432971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432971">(Sep 27 2020 at 20:24)</a>:</h4>
<p>well seems not</p>



<a name="211432974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432974">(Sep 27 2020 at 20:24)</a>:</h4>
<p>right this is integer division</p>



<a name="211432980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432980">(Sep 27 2020 at 20:25)</a>:</h4>
<p>I do have a <code>round_up</code> operation that goes up to the next multiple, and that <em>could</em> overflow.</p>



<a name="211432993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211432993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211432993">(Sep 27 2020 at 20:25)</a>:</h4>
<p>But that'd be something that integer overflow checking should catch.</p>



<a name="211433057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433057">(Sep 27 2020 at 20:26)</a>:</h4>
<p><code>num-integer</code> has <code>Integer::div_ceil</code></p>



<a name="211433063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433063">(Sep 27 2020 at 20:26)</a>:</h4>
<p>And methods for multiples too</p>



<a name="211433092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433092">(Sep 27 2020 at 20:27)</a>:</h4>
<p>That looks promising, thank you!</p>



<a name="211433095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433095">(Sep 27 2020 at 20:27)</a>:</h4>
<p>Also, <code>iN::MIN / -1</code> does overflow</p>



<a name="211433146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433146">(Sep 27 2020 at 20:28)</a>:</h4>
<p>well the example is u64</p>



<a name="211433154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433154">(Sep 27 2020 at 20:28)</a>:</h4>
<p>True enough, I had considered the negative numerator case, but not the negative denominator case.</p>



<a name="211433162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433162">(Sep 27 2020 at 20:29)</a>:</h4>
<p>unsigned integer types probably don't need overflow check</p>



<a name="211433169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433169">(Sep 27 2020 at 20:29)</a>:</h4>
<p>Ok, yeah, unsigned division can't overflow, only divide-by-0 panics</p>



<a name="211433171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lucius Hu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433171">(Sep 27 2020 at 20:29)</a>:</h4>
<p>the only error is divide by zero</p>



<a name="211433172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433172">(Sep 27 2020 at 20:29)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> Do you happen to know if <code>div_ceil</code> compiles down to decent code if given a power of two? I'm hoping that, for instance, the generated code for <code>x.div_ceil(4096)</code> involves a <code>&gt;&gt; 12</code> somewhere.</p>



<a name="211433256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433256">(Sep 27 2020 at 20:30)</a>:</h4>
<p>isn't this what <code>ptr::align_to</code> does, at least for powers of 2?</p>



<a name="211433258"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433258" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433258">(Sep 27 2020 at 20:30)</a>:</h4>
<p><code>is_multiple_of</code> is going to be nice as well.</p>



<a name="211433260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433260">(Sep 27 2020 at 20:30)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> I'm not working with pointers.</p>



<a name="211433279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433279">(Sep 27 2020 at 20:31)</a>:</h4>
<p>I'm not sure... it's not doing anything fancy, more just a method of convenience</p>



<a name="211433347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433347">(Sep 27 2020 at 20:33)</a>:</h4>
<p>LLVM will probably make that shift though</p>



<a name="211433356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433356">(Sep 27 2020 at 20:33)</a>:</h4>
<p>Yeah, I'd expect that to happen in LLVM rather than in the trait. I more wondered "is it doing anything sufficiently complex that LLVM can't see through it".</p>



<a name="211433933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211433933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211433933">(Sep 27 2020 at 20:49)</a>:</h4>
<p>Looks like it's the <a href="https://docs.rs/num-integer/0.1.43/src/num_integer/lib.rs.html#96-103">div-and-rem version</a> (which is the only overflow-safe one I know), which <a href="https://rust.godbolt.org/z/MW8z47">LLVM deals with fine</a>.  Honestly with a literal RHS I'd be very surprised if it didn't handle just about anything fine.</p>



<a name="211434170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Crate%20for%20%22divide%2C%20rounding%20up%22%3F/near/211434170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Crate.20for.20.22divide.2C.20rounding.20up.22.3F.html#211434170">(Sep 27 2020 at 20:55)</a>:</h4>
<p><span class="user-mention" data-user-id="138448">@cuviper</span> Thank you very much for pointing me to that! That's going to be very helpful.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>